<script setup lang="tsx">
import { Form, FormSchema } from '@/components/Form'
import { PropType, reactive, ref } from 'vue'
import { useValidator } from '@/hooks/web/useValidator'
import { SysTransferCenterFileRelDetailVo } from '@/api/system/file/SysTransferCenterFileRel/types'
import { isBlank } from '@/utils/StringUtils'
import { useBaseWriteView } from '@/base/views/BaseWriteView'
import { sysDictChooseListApi } from '@/api/system/dict/SysDict'
import { KeyLabelValueVo } from '@/base/api/KeyLabelValueVo'
// [[ OnflowGenerator Range (- selectFunctionImport -) Start ]]
import { selectSysTransferCenterNameListApi } from '@/api/system/file/SysTransferCenter'
import { selectSysFileFileNameListApi } from '@/api/system/file/SysFile'
// [[ OnflowGenerator Range (- selectFunctionImport -) End ]]

const { required } = useValidator()

const props = defineProps({
  currentRow: {
    type: Object as PropType<Nullable<SysTransferCenterFileRelDetailVo>>,
    default: () => null
  },
  editType: {
    type: String,
    default: 'add'
  },
  editId: {
    type: Number,
    default: -1
  }
})
const isAdd = ref(props.editType === 'add')

const { formRegister, submit, doWatch } = useBaseWriteView(props)
doWatch()

const formRef = ref<typeof Form>()
// [[ OnflowGenerator Range (- dictFunctions -) Start ]]
const executeStateOptions = ref<Array<KeyLabelValueVo>>([])
const getExecuteState = async () => {
  const res = await sysDictChooseListApi('sysTransferCenterExecuteState')
  executeStateOptions.value = res.data
}
getExecuteState()

// [[ OnflowGenerator Range (- dictFunctions -) End ]]

// [[ OnflowGenerator Range (- selectFunctions -) Start ]]
const sysTransferCenterNameList = ref<Array<KeyLabelValueVo>>([])
const getSysTransferCenterNameList = async () => {
  const res = await selectSysTransferCenterNameListApi({})
  sysTransferCenterNameList.value = res.data
}
getSysTransferCenterNameList()
const sysFileFileNameList = ref<Array<KeyLabelValueVo>>([])
const getSysFileFileNameList = async () => {
  const res = await selectSysFileFileNameListApi({})
  sysFileFileNameList.value = res.data
}
getSysFileFileNameList()

// [[ OnflowGenerator Range (- selectFunctions -) End ]]

const schema = reactive<FormSchema[]>([
  // [[ OnflowGenerator Range (- inputSchema -) Start ]]
  {
    field: 'transferCenterId',
    label: '传输中心id关联',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择传输中心id关联',
      clearable: true,
      filterable: true,
      options: sysTransferCenterNameList
    }
  },
  {
    field: 'fileId',
    label: '文件id关联',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择文件id关联',
      clearable: true,
      filterable: true,
      options: sysFileFileNameList
    }
  },
  {
    field: 'executeState',
    label: '执行状态',
    value: '0',
    component: 'Select',
    componentProps: {
      placeholder: '请选择执行状态',
      clearable: true,
      filterable: true,
      options: executeStateOptions
    }
  },
  {
    field: 'totalCount',
    label: '总行数',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入总行数',
      clearable: true
    }
  },
  {
    field: 'successCount',
    label: '成功数量',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入成功数量',
      clearable: true
    }
  },
  {
    field: 'failCount',
    label: '失败数量',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入失败数量',
      clearable: true
    }
  },
  {
    field: 'skipCount',
    label: '跳过数量',
    value: '0',
    component: 'Input',
    componentProps: {
      placeholder: '请输入跳过数量',
      clearable: true
    }
  },
  {
    field: 'errorMessage',
    label: '错误信息',
    component: 'Input',
    componentProps: {
      placeholder: '请输入错误信息',
      clearable: true
    }
  },
  {
    field: 'startTime',
    label: '处理开始时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择处理开始时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  {
    field: 'finishTime',
    label: '处理结束时间',
    component: 'DatePicker',
    componentProps: {
      placeholder: '请选择处理结束时间',
      clearable: true,
      type: 'datetime',
      valueFormat: 'YYYY-MM-DD HH:mm:ss'
    }
  },
  // [[ OnflowGenerator Range (- inputSchema -) End ]]
  {
    field: 'createTime',
    label: '创建时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无创建时间',
      disabled: true
    }
  },
  {
    field: 'updateTime',
    label: '更新时间',
    component: 'Input',
    remove: isAdd.value,
    componentProps: {
      placeholder: '暂无更新时间',
      disabled: true
    }
  }
])

const rules = reactive({
  // [[ OnflowGenerator Range (- rules -) Start ]]
  transferCenterId: [required()],
  fileId: [required()],
  executeState: [required()],
  totalCount: [required()],
  successCount: [required()],
  failCount: [required()],
  skipCount: [required()],
  errorMessage: [
    {
      validator: (_, value, callback) => {
        if (isBlank(value)) {
          callback()
          return
        }
        if (value.length > 2000) {
          callback(new Error('错误信息不能超过2000个字符'))
        } else {
          callback()
        }
      }
    }
  ]
  // [[ OnflowGenerator Range (- rules -) End ]]
})

defineExpose({
  submit
})
</script>

<template>
  <Form :rules="rules" @register="formRegister" :schema="schema" ref="formRef" />
</template>
